package com.ceba.test.modules.email.helper;


import com.ceba.cebautils.DateUtil;
import com.ceba.cebautils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ceba.test.base.language.ResourceUtils;
import com.ceba.test.modules.email.logicService.EmailLogicService;
import com.ceba.test.modules.regist.entity.Enterprise;
import com.ceba.test.modules.regist.entity.EnterpriseRegist;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import configs.context.SpringBeans;

public class EmailHelper {
	
	private static EmailLogicService emailLogicService 	
		= SpringBeans.getEmailLogicService("emailLogicService");
	
	static Logger logger = LoggerFactory.getLogger(EmailHelper.class);
	
	/**
	 * 给企业发送激活码
	 * @param enterprise
	 * @param er 
	 * @return
	 */
	public static boolean sendEnterpriseActivateEmail(Enterprise enterprise, EnterpriseRegist er) throws Exception{
		Boolean bSend = true;
		String title =ResourceUtils.getMessage("MODULES_ENTERPRISE_ACTIVATION_INFORMATION");
		String mailBody = getEmailHtmlForEnterpriseActivate(title,er);
		if (!emailLogicService.sendEnterpriseActivateCodeEmail(title, mailBody, 
				enterprise.getEnterpriseEmail())){
			bSend = false;
		}
		if(bSend){
			return true;
		}else{
			return false;
		}
	}
	
	private static String getEmailHtmlForEnterpriseActivate(String title,EnterpriseRegist er) throws Exception{
		IStore store = StoreManager.create("msg_mail_set");
		IObject object = store.get("4");
		
		String html=object.getString("CONTEXT_TEMPLATE");
		if(title.equals("")){
			title=object.getString("TITLE");
		}
		String enterName = er.getEnterpriseName();
		String useTime = "";
		String signCount = "";
		String reportVersion = "";
		if(er !=null){
			reportVersion = er.getVersion();
			if(!StringUtil.isEmptyString(reportVersion) && reportVersion.indexOf("RMS")== -1){
				String sysType = "";
				reportVersion = sysType+reportVersion; 
			}
			
			String creatTime = DateUtil.dateTrans(er.getCreateTime(), DateUtil.YYYY_MM_DD_);
			useTime = creatTime+ ResourceUtils.getMessage("MODULES_TO")+er.getAvailableDate();
			if(!StringUtil.isEmptyString(er.getNewAvailableDate())){
				useTime = creatTime+ResourceUtils.getMessage("MODULES_TO")+er.getNewAvailableDate();
			}
			if(!StringUtil.isEmptyString(er.getSignCount())){
				signCount = ResourceUtils.getMessage("MODULES_NUMBER_CURRENT_SIGNATURE_COPIES")+er.getSignCount();
			}
		}
		html=html.replace("#TITLE#", title).replace("#COMPANY#", enterName)
				.replace("#REPORT_VERSION#", reportVersion).replace("#USE_TIME#", useTime).replace("#SIGN_COUNT#", signCount);
		
		return html;
	}
	
	private static String getEmailHtmlForEnterpriseActivate(String title,
			String content, Enterprise enterprise) throws Exception{
		IStore store = StoreManager.create("msg_mail_set");
		IObject object = store.get("1");
		
		String html=object.getString("CONTEXT_TEMPLATE");
		if(title.equals("")){
			title=object.getString("TITLE");
		}
		
		html=html.replace("#TITLE#", title).replace("#CONTEXT#", content);
		return html;
	}

}
